package com.ruoyi.web.controller.procurement;

import java.io.File;
import java.io.FileInputStream;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;
import javax.servlet.http.HttpServletResponse;

import com.alibaba.excel.EasyExcel;
import com.alibaba.excel.ExcelReader;
import com.alibaba.excel.read.builder.ExcelReaderBuilder;
import com.alibaba.excel.read.metadata.ReadSheet;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.ruoyi.common.core.domain.entity.SysDictData;
import com.ruoyi.procurement.domain.doc.PsGoodsTypeDoc;
import com.ruoyi.procurement.mapper.PsGoodsTypeMapper;
import com.ruoyi.system.service.ISysDictDataService;
import com.ruoyi.system.service.ISysDictTypeService;
import io.swagger.annotations.Api;
import org.apache.commons.lang3.StringUtils;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.procurement.domain.PsGoodsType;
import com.ruoyi.procurement.service.IPsGoodsTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 物品标准Controller
 * 
 * @author ruoyi
 * @date 2023-10-10
 */
@RestController
@RequestMapping("/procurement-system/type")
public class PsGoodsTypeController extends BaseController
{
    @Autowired
    private IPsGoodsTypeService psGoodsTypeService;
    @Autowired
    private ISysDictTypeService dictTypeService;
    @Autowired
    private PsGoodsTypeMapper psGoodsTypeMapper;

    /**
     * 查询物品标准列表
     */
    @PreAuthorize("@ss.hasPermi('procurement-system:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(PsGoodsType psGoodsType)
    {
        startPage();
        List<PsGoodsType> list = psGoodsTypeService.selectPsGoodsTypeList(psGoodsType);

//        try{
//            FileInputStream fileInputStream = new FileInputStream("C:\\Users\\Administrator\\Desktop\\666\\123.xlsx");
//            List<PsGoodsTypeDoc> objects = EasyExcel.read(fileInputStream).sheet().head(PsGoodsTypeDoc.class).doReadSync();
//
//            List<SysDictData> sysDictData = dictTypeService.selectDictDataByType("ps_goods_type");
//            Map<String, SysDictData> collect = sysDictData.stream().collect(Collectors.toMap(k -> k.getDictLabel(), v -> v));
//
//            for (PsGoodsTypeDoc object : objects) {
//                PsGoodsType psGoodsType1 = new PsGoodsType();
//                LambdaQueryWrapper<PsGoodsType> psGoodsTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
//                psGoodsTypeLambdaQueryWrapper.eq(PsGoodsType::getPstName,object.getPstName());
//                PsGoodsType psGoodsTypes = psGoodsTypeMapper.selectOne(psGoodsTypeLambdaQueryWrapper);
//                if (psGoodsTypes!=null){
//                    psGoodsTypes.setPstSpecification(object.getPstSpecification());
//                    psGoodsTypeMapper.updateById(psGoodsTypes);
//                }else {
//                    psGoodsType1.setPstSpecification(object.getPstSpecification());
//                    psGoodsType1.setPstName(object.getPstName());
//                    psGoodsType1.setPstStatus(2l);
//                    SysDictData sysDictData1 = collect.get(object.getPstType());
//                    if (sysDictData1!=null){
//                        psGoodsType1.setPstType(sysDictData1.getDictValue());
//                    }
//                    psGoodsTypeMapper.insert(psGoodsType1);
//                }
//            }
//
//        }catch (Exception e){
//            throw new RuntimeException();
//        }

        return getDataTable(list);
    }

    /**
     * 导出物品标准列表
     */
    @PreAuthorize("@ss.hasPermi('procurement-system:type:export')")
    @Log(title = "物品标准", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, PsGoodsType psGoodsType)
    {
        List<PsGoodsType> list = psGoodsTypeService.selectPsGoodsTypeList(psGoodsType);
        ExcelUtil<PsGoodsType> util = new ExcelUtil<PsGoodsType>(PsGoodsType.class);
        util.exportExcel(response, list, "物品标准数据");
    }

    /**
     * 获取物品标准详细信息
     */
    @PreAuthorize("@ss.hasPermi('procurement-system:type:query')")
    @GetMapping(value = "/{id}")
    public AjaxResult getInfo(@PathVariable("id") String id)
    {
        return success(psGoodsTypeService.selectPsGoodsTypeById(id));
    }

    /**
     * 新增物品标准
     */
    @PreAuthorize("@ss.hasPermi('procurement-system:type:add')")
    @Log(title = "物品标准", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody PsGoodsType psGoodsType)
    {
        return toAjax(psGoodsTypeService.insertPsGoodsType(psGoodsType));
    }

    /**
     * 导出物品标准列表
     */
    @PreAuthorize("@ss.hasPermi('procurement-system:type:export')")
    @Log(title = "物品标准导入", businessType = BusinessType.EXPORT)
    @PostMapping("/importw")
    public void importw(MultipartFile file)
    {
        try(ExcelReader build = EasyExcel.read(file.getInputStream()).build()) {
            List<PsGoodsTypeDoc> objects = EasyExcel.read(file.getInputStream()).head(PsGoodsTypeDoc.class).sheet().doReadSync();
            List<SysDictData> sysDictData = dictTypeService.selectDictDataByType("ps_goods_type");
            Map<String, SysDictData> collect = sysDictData.stream().collect(Collectors.toMap(k -> k.getDictValue(), v -> v));

            for (PsGoodsTypeDoc object : objects) {
                PsGoodsType psGoodsType = new PsGoodsType();
                LambdaQueryWrapper<PsGoodsType> psGoodsTypeLambdaQueryWrapper = new LambdaQueryWrapper<>();
                psGoodsTypeLambdaQueryWrapper.like(PsGoodsType::getPstName,object.getPstName());
                PsGoodsType psGoodsTypes = psGoodsTypeMapper.selectOne(psGoodsTypeLambdaQueryWrapper);
                if (psGoodsTypes!=null){
                    psGoodsTypes.setPstSpecification(object.getPstSpecification());
                }else {
                    psGoodsType.setPstSpecification(object.getPstSpecification());
                    psGoodsType.setPstName(object.getPstName());
                    psGoodsType.setPstStatus(2l);
                    SysDictData sysDictData1 = collect.get(object.getPstType());
                    if (sysDictData1!=null){
                        psGoodsType.setPstType(sysDictData1.getDictValue());
                    }
                }
            }

            System.out.println(objects);
        }catch (Exception e){

        }
    }

    /**
     * 修改物品标准
     */
    @PreAuthorize("@ss.hasPermi('procurement-system:type:edit')")
    @Log(title = "物品标准", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody PsGoodsType psGoodsType)
    {
        return toAjax(psGoodsTypeService.updatePsGoodsType(psGoodsType));
    }

    /**
     * 删除物品标准
     */
    @PreAuthorize("@ss.hasPermi('procurement-system:type:remove')")
    @Log(title = "物品标准", businessType = BusinessType.DELETE)
	@DeleteMapping("/{ids}")
    public AjaxResult remove(@PathVariable String[] ids)
    {
        return toAjax(psGoodsTypeService.deletePsGoodsTypeByIds(ids));
    }
}
